package blog.open1111.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;

/**
 * Created by dengwubo on 2017/9/20.
 */
public class OSSUtils {

    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId ="LTAI36aSn8awo3s5";
    private static String accessKeySecret ="yTO3O1zJh7WyjjzEGHecaqSIh5TDNK";
    private static String bucketName = "dengwuboobblog";
    private static  OSSClient client;
    private String filedir = "data/";
    public static String uploadFile(String key,InputStream image,String ext)throws  Exception{

        client = new OSSClient(endpoint,accessKeyId,accessKeySecret);
        //String fileName = key+"."+ext;
   /*     System.out.println("file name :"+fileName);
        File file = new File("D:\\"+fileName);
        StreamTool.streamSaveAsFile(image,file);*/



        client.putObject(bucketName,key,image);

        OSSObject object = client.getObject(bucketName, key);
        String uri = object.getResponse().getUri();
        System.out.println(uri);
        return uri;
    }

    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @param FilenameExtension 文件后缀
     * @return String
     */
    public static String getcontentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase("bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase("gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase("jpeg") ||
                FilenameExtension.equalsIgnoreCase("jpg") ||
                FilenameExtension.equalsIgnoreCase("png")) {
            return "image/jpeg";
        }
        if (FilenameExtension.equalsIgnoreCase("html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase("txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase("vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase("pptx") ||
                FilenameExtension.equalsIgnoreCase("ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase("docx") ||
                FilenameExtension.equalsIgnoreCase("doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase("xml")) {
            return "text/xml";
        }
        return "image/jpeg";
    }


}
